package ru.ee;

import javax.microedition.khronos.opengles.GL10;

import ru.ee.objects.LinePointer;
import ru.ee.objects.LoadingBg;
import ru.ee.objects.RepeatBtn;
import ru.ee.objects.StandartBg;
import ru.ee.objects.StringBtn;
import ru.ee.objects.StringPanel;

import android.hardware.SensorEvent;
import android.view.MotionEvent;
import framework.Game;
import framework.GameScreen;

public class MainScreen extends GameScreen {

	public MainScreen(Game game) {
		super(game);
	}

	@Override
	public void handleAccelerometerEvent(SensorEvent arg0) {
	}

	@Override
	public void handleTouchEvent(MotionEvent event) {
		standart.handle(event);
		plus1.handle(event);
		plus2.handle(event);
		plus3.handle(event);
		plus4.handle(event);
		minus1.handle(event);
		minus2.handle(event);
		minus3.handle(event);
		minus4.handle(event);
		repeatBtn.handle(event);
	}

	private StandartBg bg;
	private StringPanel standart;
	private StringPanel plus1;
	private StringPanel plus2;
	private StringPanel plus3;
	private StringPanel plus4;
	private StringPanel minus1;
	private StringPanel minus2;
	private StringPanel minus3;
	private StringPanel minus4;
	
	private RepeatBtn repeatBtn;
	
	private LinePointer linePointer;
	
	private LoadingBg loadingBg;
	
	@Override
	protected void initScreenObjects() {
		bg = new StandartBg();
		loadingBg = new LoadingBg();
		float basicX = 130;
		float basicDX = 90;
		float basicY = 560;
		standart = new StringPanel(this, "standart", basicX, basicY, basicDX);
		plus1 = new StringPanel(this, "plus1", basicX, basicY + 80, basicDX);
		plus2 = new StringPanel(this, "plus2", basicX, basicY + 80 * 2, basicDX);
		plus3 = new StringPanel(this, "plus3", basicX, basicY + 80 * 3, basicDX);
		plus4 = new StringPanel(this, "plus4", basicX, basicY + 80 * 4, basicDX);
		minus1 = new StringPanel(this, "minus1", basicX, basicY - 80, basicDX);
		minus2 = new StringPanel(this, "minus2", basicX, basicY - 80 * 2, basicDX);
		minus3 = new StringPanel(this, "minus3", basicX, basicY - 80 * 3, basicDX);
		minus4 = new StringPanel(this, "minus4", basicX, basicY - 80 * 4, basicDX);
		repeatBtn = new RepeatBtn(this, null, 300, 150);
		linePointer = new LinePointer(basicY);
	}

	@Override
	public void present(GL10 gl, float deltaTime) {
		if (!Game.SOUND_READY) {
			loadingBg.draw(gl, deltaTime);
			return;
		}
		bg.draw(gl, deltaTime);
		linePointer.draw(gl, deltaTime);
		standart.draw(gl, deltaTime);
		plus1.draw(gl, deltaTime);
		plus2.draw(gl, deltaTime);
		plus3.draw(gl, deltaTime);
		plus4.draw(gl, deltaTime);
		minus1.draw(gl, deltaTime);
		minus2.draw(gl, deltaTime);
		minus3.draw(gl, deltaTime);
		minus4.draw(gl, deltaTime);
		repeatBtn.draw(gl, deltaTime);
	}
	
	private float timePassed = 0;
	private float soundInterval = 4;
	private StringBtn currentBtn;

	@Override
	public void update(float deltaTime) {
		if (!Game.SOUND_READY) return;
		if (currentBtn == null) return;
		if (!repeatModeOn) return;
		timePassed += deltaTime;
		if (timePassed > soundInterval) {
			timePassed = 0;
			currentBtn.playSound();
		}
	}
	
	public void setCurrentBtn(StringBtn btn) {
		if (currentBtn != null) {
			currentBtn.disactivate();
		}
		timePassed = 0;
		currentBtn = btn;
		currentBtn.activate();
	}
	
	private boolean repeatModeOn = true;
	
	public void changeRepeatMode() {
		repeatModeOn = !repeatModeOn;
	}
	
	public void setCurrentLineY(float y) {
		linePointer.setY(y);
	}

}
